package cn.justweb.StudentManager;

import java.util.ArrayList;
import java.util.Scanner;


/*
--------欢迎来到学生管理系统--------
1 添加学生
2 删除学生
3 修改学生
4 查看学生
5 退出
请输入您的选择:
* */
public class Manager {
    public static void main(String[] args) {
        ArrayList<Student> list=new ArrayList<>();
        Scanner sc=new Scanner(System.in);
        a:while(true) {
            System.out.println("--------欢迎来到学生管理系统--------");
            System.out.println("1 添加学生");
            System.out.println("2 删除学生");
            System.out.println("3 修改学生");
            System.out.println("4 查看学生");
            System.out.println("5 退出");
            System.out.println("请输入您的选择:");

            String choice = sc.nextLine();
            switch (choice){
                case "1":
                    addStudent(list);
                    break;
                case "2":
                    delStudent(list);
                    break;
                case "3":
                    updateStudent(list);
                    break;
                case "4":
                    queryStudent(list);
                    break;
                case "5":
                    break a;
                default:
                    System.out.println("输入错误，请重新输入!");
                    break;
            }
        }
    }

    private static void queryStudent(ArrayList<Student> list) {
        if(list.size()==0){
            System.out.println("无信息，请先添加学生信息");
        }else{
            System.out.println("学号"+"\t"+"姓名"+"\t"+"年龄"+"\t"+"生日");
            for (int i = 0; i < list.size(); i++) {
                Student student=list.get(i);
                System.out.println(student.getStuNo()+"\t\t"+student.getStuName()+"\t\t"+student.getStuAge()+"\t\t"+student.getBrith());
            }
        }
    }

    private static void updateStudent(ArrayList<Student> list) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入要修改的学生学号");
        String stuNo = sc.nextLine();
        int index=getIndex(list,stuNo);
        if(index==-1){
            System.out.println("找不到该学生,请重新输入！");
            return;
        }else {
            System.out.println("请输入姓名");
            String stuName = sc.nextLine();
            System.out.println("请输入年龄");
            String stuAge = sc.nextLine();
            System.out.println("请输入生日");
            String birth = sc.nextLine();
            Student student=new Student(stuNo,stuName,stuAge,birth);
            list.set(index,student);
            System.out.println("修改成功");
        }
    }

    private static void delStudent(ArrayList<Student> list) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入要删除的学生学号");
        String stuNo = sc.nextLine();
        int index=getIndex(list,stuNo);
        if(index==-1){
            System.out.println("找不到该学生,请重新输入！");
            return;

        }else {
            list.remove(index);
            System.out.println("删除成功");
        }
    }

    private static void addStudent(ArrayList<Student> list) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入学号");
        String stuNo = sc.nextLine();
        int index=getIndex(list,stuNo);
        if(index==-1){

        }else {
            System.out.println("该学生已存在,请重新输入！");
            return;
        }
        System.out.println("请输入姓名");
        String stuName = sc.nextLine();
        System.out.println("请输入年龄");
        String stuAge = sc.nextLine();
        System.out.println("请输入生日");
        String birth = sc.nextLine();
        Student student=new Student(stuNo,stuName,stuAge,birth);
        list.add(student);
        System.out.println("添加成功");
        System.out.println(list);
    }

    private static int getIndex(ArrayList<Student> list, String stuNo) {
        for (int i = 0; i < list.size(); i++) {
            Student student=list.get(i);
            String stuNo1=student.getStuNo();
            if(stuNo.equals(stuNo1)){
                return i;
            }
        }
        return -1;
    }
}